﻿package me.zhengjie.common.exception.handler

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.annotation.JsonTypeName
import me.zhengjie.common.utils.JacksonUtil
import java.time.LocalDateTime


/**
 * @author Kuki
 * @date 2024-11-10
 */
@JsonTypeName("ApiError")
data class ApiError (
    var status: Int? = 400,

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    var timestamp: LocalDateTime = LocalDateTime.now(),
    var message: String? = null,

){
    annotation class Update

    companion object {
        fun error(message: String?): ApiError {
            val apiError = ApiError()
            apiError.message = message
            return apiError
        }

        fun error(status: Int?, message: String?): ApiError {
            val apiError = ApiError()
            apiError.status = status!!
            apiError.message = message
            return apiError
        }
    }
/*        fun fromJson(json: String): ApiError? {
            val obj=JacksonUtil.parseObject(json, ApiError::class.java)!!
            return when(obj){
                is ApiError -> obj
                else -> null
            }
        }
    }


    fun tojson():String? {
        return JacksonUtil.toJSONString(this)
    }

    fun copy(source: ApiError?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }*/
}


